package com.koi.system.controller.loginlog;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.anno.KoiLog;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.model.vo.system.SysLoginLogVo;
import com.koi.pojo.system.SysLoginLog;
import com.koi.system.service.loginlog.SysLoginLogService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ･ᴗ･
 * @description [系统访问记录-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysLoginLogController {

    private final SysLoginLogService sysLoginLogService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:loginlog:list")
    @GetMapping("/sysLoginLog/listPage")
    public IPage<SysLoginLog> listPage(SysLoginLogVo vo) {
        // 分页构造器
        Page<SysLoginLog> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(vo.getLoginName()), SysLoginLog::getLoginName, vo.getLoginName());
        wrapper.like(StringUtils.isNotBlank(vo.getIpAddress()), SysLoginLog::getIpAddress, vo.getIpAddress());
        wrapper.eq(StringUtils.isNotBlank(vo.getLoginStatus()), SysLoginLog::getLoginStatus, vo.getLoginStatus());
        wrapper.ge(StringUtils.isNotBlank(vo.getBeginTime()), SysLoginLog::getLoginTime, vo.getBeginTime());
        wrapper.le(StringUtils.isNotBlank(vo.getEndTime()), SysLoginLog::getLoginTime, vo.getEndTime());
        wrapper.orderByDesc(SysLoginLog::getLoginTime);
        // 进行分页查询
        page = sysLoginLogService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysLoginLog/list")
    public List<SysLoginLog> list() {
        return sysLoginLogService.list();
    }

    /**
     * @description [查询一个]
     * @author ･ᴗ･
     */
    @GetMapping("/sysLoginLog/getById/{id}")
    public SysLoginLog getById(@PathVariable Long id) {
        return sysLoginLogService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:loginlog:add")
    @PostMapping("/sysLoginLog/add")
    public void add(@Validated @RequestBody SysLoginLog sysLoginLog) {
        YUtil.isTrue(!sysLoginLogService.save(sysLoginLog), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:loginlog:update")
    @PostMapping("/sysLoginLog/update")
    public void update(@Validated @RequestBody SysLoginLog sysLoginLog) {
        YUtil.isTrue(!sysLoginLogService.updateById(sysLoginLog), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除登录日志", operType = OperType.DELETE)
    @SaCheckPermission("system:loginlog:delete")
    @PostMapping("/sysLoginLog/deleteById/{id}")
    public void delete(@PathVariable("id") Long id) {
        YUtil.isTrue(!sysLoginLogService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "批量删除登录日志", operType = OperType.DELETE)
    @SaCheckPermission("system:loginlog:delete")
    @PostMapping("/sysLoginLog/batchDelete")
    public void batchDelete(@NotNull(message = "请选择需要删除的数据") @RequestBody List<Long> ids) {
        YUtil.isTrue(!sysLoginLogService.removeByIds(ids), "删除失败，请稍后重试");
    }

}
